package cn.yuanqiao.feignService;

import cn.yuanqiao.archive.appManager.domain.TenantSysDictData;
import cn.yuanqiao.archive.appManager.service.*;
import cn.yuanqiao.archive.archiveBorrow.domain.YqDaBorrow;
import cn.yuanqiao.archive.archiveBorrow.service.IYqDaBorrowService;
import cn.yuanqiao.archive.bianjifabu.service.IYqfabufanganService;
import cn.yuanqiao.archive.danganzhengli.service.IHandOverReceiveService;
import cn.yuanqiao.archive.danganzhengli.service.IOnlineReceptionService;
import cn.yuanqiao.archive.detection.service.IDetectionObjectService;
import cn.yuanqiao.archive.sealmanage.service.ISealmanageService;
import cn.yuanqiao.archive.storeRoom.service.InitializationService;
import cn.yuanqiao.common.annotation.Log;
import cn.yuanqiao.common.core.controller.BaseController;
import cn.yuanqiao.common.core.domain.AjaxResult;
import cn.yuanqiao.common.core.domain.R;
import cn.yuanqiao.common.core.domain.TreeSelect;
import cn.yuanqiao.common.core.page.TableDataInfo;
import cn.yuanqiao.common.domain.YqField;
import cn.yuanqiao.common.domain.YqTable;
import cn.yuanqiao.common.enums.BusinessType;
import cn.yuanqiao.common.utils.DateUtils;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static cn.yuanqiao.common.utils.PageUtils.startPage;

/**
 * @ClassName: RemoteCommonService
 * @Description: 类的描述
 * @author: lihuahua
 * @date: 2024/7/8 16:03
 * @Blog: huahuacoco.top
 */

@Service
public class RemoteCommonService extends BaseController {

    @Autowired
    private InitializationService initializationService;

    @Resource
    private IHandOverReceiveService handOverReceiveService;

    @Autowired
    private IYqDaBorrowService yqDaBorrowService;

    @Autowired
    private ISealmanageService SealmanageService;

    @Autowired
    private IOnlineReceptionService onlineReceptionService;

    @Autowired
    private ITenantMijiComparisonTableService iTenantMijiComparisonTableService;

    @Autowired
    private IDetectionObjectService detectionObjectService;

    @Autowired
    private IYqfabufanganService YqfabufanganService;

    @Autowired
    private IYqdaTreesService yqdaTreesService;

    @Autowired
    private ITenantCateMdataListService tenantMdataListService;

    @Autowired
    private ITenantSysConfigService tenantSysConfigService;

    @Autowired
    private ITenantSysDictTypeService dictTypeService;


    public R<List<YqTable>> getTableList() {
        List<YqTable> tableList = initializationService.getTableList();
        return R.ok(tableList);
    }

    public R<TableDataInfo> getFieldList( String tableId, String pageNum, String pageSize) {
        startPage();
        List<YqField> fieldList = initializationService.getFieldList(tableId);
        return R.ok(getDataTable(fieldList));
    }

    @Log(title = "移交办结", businessType = BusinessType.UPDATE)
    public R<Integer> yijiaoBanjie( Long id) {
        int i = handOverReceiveService.yijiaoBanjie(id);
        return R.ok(Integer.valueOf(i));
    }

    /**
     * 审批流程不同意
     *
     * @param id 移交单ID
     * @return
     */
    @Log(title = "审批流程不同意", businessType = BusinessType.UPDATE)
    public AjaxResult killHOV( Long id) {
        handOverReceiveService.killHOV(id);
        return AjaxResult.success();
    }


    /**
     * 修改archiveBorrow
     */
    @Log(title = "archiveBorrow", businessType = BusinessType.UPDATE)
    public R<Integer> updateArchiveBorrowDate(Long borrowId, Long borrowDays,Integer status) {
        YqDaBorrow yqDaBorrow = new YqDaBorrow();
        yqDaBorrow.setBorrowId(borrowId);
        if (borrowDays != null) {
            yqDaBorrow.setStartTime(DateUtils.getNowDate());
            yqDaBorrow.setEndTime(DateUtils.addDate(DateUtils.getNowDate(), borrowDays));
        }
        if (status != null) {
            yqDaBorrow.setStatus(status);
        }
        return R.ok(yqDaBorrowService.updateArchiveBorrowDate(yqDaBorrow));
    }

    /**
     * 修改archiveBorrow
     */
    @Log(title = "archiveBorrow", businessType = BusinessType.UPDATE)
    public R<Integer> updateAllStatus(Long userId) {
        return R.ok(yqDaBorrowService.updateAllStatus(userId));
    }

    public R<String> getImagePath(Long id, String map)
    {
        String path = SealmanageService.getImagePath(id,map);
        return R.ok(path);
    }

    @Log(title = "在线接收审批流程不同意", businessType = BusinessType.UPDATE)
    public AjaxResult killOnlineReceive( Long id) {
        onlineReceptionService.killOnlineReceive(id);
        return AjaxResult.success();
    }

    @Log(title = "在线接收办结", businessType = BusinessType.UPDATE)
    public R<Integer> onlinereceiveBanjie( Long id) {
        int i = onlineReceptionService.onlinereceiveBanjie(id);
        onlineReceptionService.sendPackageState(id,"success");
        return R.ok(Integer.valueOf(i));
    }

    @Log(title = "根据人员密级获取能查看的档案密级", businessType = BusinessType.SELECT)
    public R<AjaxResult> getdamjbyrymjforsys(){
        return R.ok(success(iTenantMijiComparisonTableService.getdamjbyrymjforsys()));
    }

    @Log(title = "四性检测", businessType = BusinessType.OTHER)
    public AjaxResult detectionPackage( JSONObject json){
        String path=json.getString("path");
        String code=json.getString("code");
        String link=json.getString("link");
        String lx=json.getString("lx");
        return AjaxResult.success(detectionObjectService.detectionPackage(path,code,link,lx));
    }

    @Log(title = "获取完整的档案树feign", businessType = BusinessType.SELECT)
    public R<List<TreeSelect>> getdanganTree(){
        return R.ok(yqdaTreesService.getTreeList());
    }

    @Log(title = "feign获取展示字段",businessType = BusinessType.SELECT)
    public R<List<Map<String,String>>> getshowFields( JSONObject json)
    {
        List<Map<String,String>> list=tenantMdataListService.getshowfield(json);
        return R.ok(list);
    }

    @Log(title = "feign获取查询字段",businessType = BusinessType.SELECT)
    public R<List<Map<String,String>>> getQueryFields( JSONObject json)
    {
        List<Map<String,String>> list=tenantMdataListService.getshowfield(json);
        return R.ok(list);
    }

    public AjaxResult getorder(JSONObject json)
    {
        String orderstr=tenantMdataListService.getorder(json.getLong("categoryId"),json.getLong("menuId"),json.getInteger("type"));
        return success(orderstr);
    }

    @Log(title = "查询所有的字典",businessType = BusinessType.SELECT)
    public R<Map<String,String>> getDictDataType(String code,String tenantCode)
    {
        List<TenantSysDictData> dictDatalist = dictTypeService.selectDictDataByTypeCode(code, tenantCode);
        Map<String,String> map=new HashMap<>();
        for(TenantSysDictData dictData:dictDatalist){
            map.put(dictData.getDictValue(),dictData.getDictLabel());
        }

        return R.ok(map);
    }

    //获取所有的值
    @Log(title = "查询参数值",businessType = BusinessType.SELECT)
    public R<String> getSysConfig(String configKey,String tenantCode)
    {
        String code=tenantSysConfigService.selectConfigByCode(configKey,tenantCode);
        return R.ok(code);
    }

    public AjaxResult updateFanganState( String fanganId,String state)
    {
        return toAjax(YqfabufanganService.updateState(fanganId,state));
    }

}
